package pers.hubb.user.service;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.dubbo.config.annotation.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pers.hubb.entity.user.UserEntity;
import pers.hubb.exception.CommonBizException;
import pers.hubb.exception.ExpCodeEnum;
import pers.hubb.facade.user.UserService;
import pers.hubb.req.user.LoginReq;
import pers.hubb.req.user.UserQueryReq;

/**
 * @author hubb
 * @Description
 * @Date 2018/4/2 23:40
 */
@Service(version = "1.0.0")
@org.springframework.stereotype.Service
public class UserServiceImpl implements UserService {

	public static Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

	/*@Autowired
	private UserDAO userDAO;*/

	@Override
	public UserEntity login(LoginReq loginReq) {
		UserEntity userEntity = new UserEntity();
		userEntity.setId("1111");
		userEntity.setUsername("张三");
		return userEntity;
		/*//参数校验
		checkParam(loginReq);
		//创建用户查询请求
		UserQueryReq userQueryReq = buildUserQueryReq(loginReq);
		// 查询用户
		List<UserEntity> userEntityList = userDAO.findUsers(userQueryReq);
		// 查询失败
		if (CollectionUtils.isEmpty(userEntityList)) {
			throw new CommonBizException(ExpCodeEnum.LOGIN_FAIL);
		}
		// 查询成功
		return userEntityList.get(0);*/
	}

	/**
	 * 参数校验
	 *
	 * @param loginReq
	 */
	private void checkParam(LoginReq loginReq) {
		// 密码不能为空
		if (StringUtils.isEmpty(loginReq.getPassword())) {
			throw new CommonBizException(ExpCodeEnum.PASSWD_NULL);
		}

		// 手机、mail、用户名 至少填一个
		if (StringUtils.isEmpty(loginReq.getUsername()) &&
				StringUtils.isEmpty(loginReq.getMail()) &&
				StringUtils.isEmpty(loginReq.getPhone())) {
			throw new CommonBizException(ExpCodeEnum.AUTH_NULL);
		}
	}

	/**
	 * 构造用户查询对象
	 *
	 * @param loginReq 登录请求
	 * @return 用户查询请求
	 */
	private UserQueryReq buildUserQueryReq(LoginReq loginReq) {
		UserQueryReq userQueryReq = new UserQueryReq();
		// 设置密码
		userQueryReq.setPassword(loginReq.getPassword());
		// 设置用户名
		if (StringUtils.isNotEmpty(loginReq.getUsername())) {
			userQueryReq.setUsername(loginReq.getUsername());
		}
		// 设置邮箱
		if (StringUtils.isNotEmpty(loginReq.getMail())) {
			userQueryReq.setMail(loginReq.getMail());
		}
		// 设置手机号
		if (StringUtils.isNotEmpty(loginReq.getPhone())) {
			userQueryReq.setPhone(loginReq.getPhone());
		}

		return userQueryReq;
	}
}
